package com.数据结构;

public class MyHashMap {
  private Entry[] entries = new Entry[4];
  private int size = 0;

  public void put(Object key, Object value) {
    int hashcode = key.hashCode();
    //        int index = hashcode % entries.length;
    int index = hashcode & (entries.length - 1);
    Entry entry = new Entry(key, value);
    if (entries[index] == null) {
      entries[index] = entry;
    } else {
      for (Entry en : entries) {
        if(en.getNext() == null | en.getKey().equals(key)){
          en.setNext(entry);
        }
      }
    }
    size += 1;
  }

  public void remove(Object key){
    int hashcode = key.hashCode();
    int index = hashcode & (entries.length - 1);
    if(entries[index].equals(key)){
      entries[index] = entries[index].getNext();
    }else{
      if(entries[index].getNext().getKey().equals(key)){
        entries[index].setNext(entries[index].getNext().getNext());
      }
    }
  }


  public Object get(Object key) {
    int index = key.hashCode() & (entries.length - 1);
    return entries[index].getValue();
  }

  public static void main(String[] args) {
    MyHashMap map = new MyHashMap();
    map.put("aaa", 111);
    map.put("bbb", 222);
    map.put("ccc", 333);
    Integer value = (Integer) map.get("bbb");
    System.out.println(value);
  }
}

class Entry {

  private Object key;

  private Object value;

  private Entry next;

  public Entry(Object key, Object value) {
    this.key = key;
    this.value = value;
  }

  public Entry getNext() {
    return next;
  }

  public void setNext(Entry next) {
    this.next = next;
  }

  public Object getKey() {
    return key;
  }

  public void setKey(Object key) {
    this.key = key;
  }

  @Override
  public String toString() {
    return "Entry{" + "key=" + key + ", value=" + value + '}';
  }

  public Object getValue() {
    return value;
  }

  public void setValue(Object value) {
    this.value = value;
  }
}
